//
//  STLimitController.m
//  CuiBoStore
//
//  Created by 崔博 on 16/11/28.
//  Copyright © 2016年 崔博. All rights reserved.
//

#import "STLimitController.h"
#import "STLimitHeaderView.h"
#import "STLimitHandle.h"
#import "STTableViewCell.h"
#import "STLimitBrandCell.h"
#import "STLimitBrandModel.h"
#import "STProductDetailController.h"
#import "STSingleModel.h"
@interface STLimitController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UIScrollView *backScro;
@property (nonatomic, strong) UITableView *singleProductTable;
@property (nonatomic, strong) UITableView *brandTable;
@property (nonatomic, strong) STLimitHeaderView *headerView;
@property (nonatomic, strong) NSArray *singleDataList;
@property (nonatomic, strong) NSArray *brandDataList;
@end

@implementation STLimitController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    [self setUpUI];
    // 请求轮播
    [self loadBanner];
    [self loadGroup];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barItem:@"限时特卖界面搜索按钮" title:@"" target:self action:@selector(rightClick)];
    
}
#pragma mark - mothed
// 请求轮播
- (void)loadBanner
{
    [STLimitHandle excuteBannerSucess:^(id obj) {
        self.headerView.bannerArray = obj;
    } failed:^(id obj) {
    }];
    
    [STLimitHandle excuteSingleListSucess:^(id obj) {
        self.singleDataList = obj;
        _singleProductTable.height = self.singleDataList.count * 170;
        _backScro.contentSize = CGSizeMake(0, self.singleDataList.count * 170 + 280);
        [_singleProductTable reloadData];
    } failed:^(id obj) {
        
    }];
}
// 请求团购
- (void)loadGroup{
    [STLimitHandle excuetBrandListSucess:^(id obj) {
        self.brandDataList = obj;
        [_brandTable reloadData];
    } failed:^(id obj) {
        
    }];
}
// 布局子控件
- (void)setUpUI{
    [self.view addSubview:self.backScro];
    [self.backScro addSubview:self.headerView];
    [self.backScro addSubview:self.singleProductTable];
    [self.backScro addSubview:self.brandTable];
}
#pragma mark - event
// right
- (void)rightClick
{
    UIViewController *searchVC = [[NSClassFromString(@"STSearchViewController") alloc] init];
    [self.navigationController pushViewController:searchVC animated:YES];
}
#pragma mark - delegate
#pragma mark tableDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (tableView == self.singleProductTable) {
        return self.singleDataList.count;
    }else
    {
        return self.brandDataList.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (tableView == self.singleProductTable) {
        STTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.singleModel      = self.singleDataList[indexPath.row];
        return cell;
    }else{
        STLimitBrandCell *cell = [tableView dequeueReusableCellWithIdentifier:@"brandcell"];
        STLimitBrandModel *m   = self.brandDataList[indexPath.row];
        cell.imageUrl          = m.LogoImg;
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (tableView == self.singleProductTable) {
        STProductDetailController *detail = [[STProductDetailController alloc] init];
        STSingleModel *m = self.singleDataList[indexPath.row];
        detail.detailsGoodsId  = m.GoodsId;
        [self.navigationController pushViewController:detail animated:YES];
    }else
    {
        
    }
}
#pragma mark - lazy
- (UIScrollView *)backScro
{
    if (!_backScro) {
        _backScro = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT - 69)];
    }
    return _backScro;
}
- (UIView *)headerView
{
    if (!_headerView) {
        _headerView = [STLimitHeaderView limitHeaderView];
        @weakify(self);
        _headerView.block = ^(NSInteger index){
            [UIView animateWithDuration:0.5 animations:^{
                if (index == 1) {
                    weak_self.singleProductTable.x = 0;
                    weak_self.brandTable.x = SCREEN_WIDTH;
                    weak_self.singleProductTable.height = self.singleDataList.count * 170;
                    weak_self.backScro.contentSize = CGSizeMake(0, self.singleDataList.count * 170 + 280);
                }else if(index == 2)
                {
                     weak_self.backScro.contentSize = CGSizeMake(0, self.brandDataList.count * 200 + 280);
                    weak_self.singleProductTable.x = -SCREEN_WIDTH;
                    weak_self.brandTable.x = 0;
                    weak_self.brandTable.height = self.brandDataList.count * 200;
                    [weak_self.brandTable reloadData];
                }
            }];
        };
    }
    return _headerView;
}

- (UITableView *)singleProductTable
{
    if (!_singleProductTable) {
        _singleProductTable = [STUIKitTools tableDelegate:self dataSource:self];
        _singleProductTable.frame = CGRectMake(0, self.headerView.maxY, SCREEN_WIDTH,SCREENH_HEIGHT);
        _singleProductTable.rowHeight = 170;
        [_singleProductTable registerNib:[UINib nibWithNibName:@"STTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    }
    return _singleProductTable;
}

- (UITableView *)brandTable
{
    if (!_brandTable) {
        _brandTable = [STUIKitTools tableDelegate:self dataSource:self];
        _brandTable.frame = CGRectMake(SCREEN_WIDTH, self.headerView.maxY, SCREEN_WIDTH, 1700);
        _brandTable.rowHeight = 200;
        [_brandTable registerNib:[UINib nibWithNibName:@"STLimitBrandCell" bundle:nil] forCellReuseIdentifier:@"brandcell"];
    }
    return _brandTable;
}

@end
